package com.dcy.common.utils;

import cn.hutool.core.util.StrUtil;
import com.dcy.common.constant.Constant;
import org.apache.dubbo.rpc.RpcContext;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author：dcy
 * @Description: 用户信息设置
 * @Date: 2021/4/15 15:28
 */
public final class UserInfoUtil {

    /**
     * 设置用户信息
     *
     * @param request
     */
    public static void setUserInfo(HttpServletRequest request) {
        String userId = request.getHeader(Constant.USER_ID);
        String username = request.getHeader(Constant.USER_NAME);
        if (StrUtil.isNotBlank(userId)) {
            RpcContext.getContext().setAttachment(Constant.USER_ID, userId);
        }
        if (StrUtil.isNotBlank(username)) {
            RpcContext.getContext().setAttachment(Constant.USER_NAME, username);
        }
    }

    /**
     * 清空信息
     */
    public static void clearUserInfo() {
        RpcContext.getContext().clearAttachments();
    }
}
